#include "mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

        //设置窗口大小与名称
        this->setGeometry(0,0,800,600);
        this->setWindowTitle("V4l2 Camera");

        //生成各类窗口部件
        ShowLabel = new QLabel;
        PhotographBtn = new QPushButton;
        VideoBtn = new QPushButton;
        OptionsBtn = new QPushButton;
        PhotoPreviewBtn = new QPushButton;
        ExitBtn = new QPushButton;
        VideoPreviewBtn = new QPushButton;
        ShowLabel->setSizeIncrement(400,300);

        //ShowLabel->setText("Show Videos...");
        ShowLabel->setAlignment(Qt::AlignCenter);
        PhotographBtn->setText("Photograph");
        VideoBtn->setText("Video");
        OptionsBtn->setText("Options");
        PhotoPreviewBtn->setText("PhotoPreview");
        ExitBtn->setText("Exit");
        VideoPreviewBtn->setText("VideoPerview");

       AllLayout = new QVBoxLayout;
       BtnLayout = new QHBoxLayout;

       BtnLayout->addWidget(PhotographBtn);
       BtnLayout->addWidget(PhotoPreviewBtn);
       BtnLayout->addWidget(VideoBtn);
       BtnLayout->addWidget(VideoPreviewBtn);
       BtnLayout->addWidget(OptionsBtn);

       BtnLayout->addWidget(ExitBtn);

       AllLayout->addWidget(ShowLabel);
       AllLayout->addLayout(BtnLayout);

       widget = new QWidget(this);
       widget->setLayout(AllLayout);
       this->setCentralWidget(widget);

       deviceInit();
       //m_v4l2 = new V4L2;
       //设置信号与槽
       connect(ExitBtn,SIGNAL(clicked()),this,SLOT(close()));
       //下面直接复制过来的,还没弄完,最后的close槽到头文件去声明,然后替换
       connect(PhotographBtn,SIGNAL(clicked()),this,SLOT(Photograph()));
       connect(VideoBtn,SIGNAL(clicked()),this,SLOT(Video()));
       connect(OptionsBtn,SIGNAL(clicked()),this,SLOT(Options()));
       connect(PhotoPreviewBtn,SIGNAL(clicked()),this,SLOT(PhotoPreview()));
       connect(VideoPreviewBtn,SIGNAL(clicked()),this,SLOT(VideoPreview()));


       //实时显示功能
       //使用QTimer定时器实现
       QTimer *timer = new QTimer(this);
       connect(timer,SIGNAL(timeout()),this,SLOT(showcamera()));
       timer->start(33);


}

void MainWindow::deviceInit(void)
{
    //创建文件系统
    pf.CreateFileDirectory();
    //初始化ffmpeg
    ffmpeg.InitFfmpeg();
    //ffmpeg开始采集
    ffmpeg.CaptureFfmpeg();
    //数据库初始化
    setParameter.qtSqlInit();
}

MainWindow::~MainWindow()
{
    //delete w;
    ffmpeg.UninitFfmpeg();
}

void MainWindow::receivelogin()
{
    this->show();
}


void MainWindow::Photograph()
{
    screenthread.start();
}


void MainWindow::Video()
{
    recordthread.start();
}

void MainWindow::Options()
{
    setParameter.show();
    setParameter.exec();

}

void MainWindow::PhotoPreview()
{
    screen.filterInit();
    this->hide();
    screen.show();
    screen.exec();
    this->show();
}

void MainWindow::VideoPreview()
{
    recording.recordFilterInit();
    this->hide();
    recording.show();
    recording.exec();
    this->show();

}

void MainWindow::showcamera()
{
    ShowLabel->setPixmap(ffmpeg.realTimeDisplay());
}
